package shoppingmall.reception.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shoppingmall.reception.mapper.MemberAddressMapper;

import shoppingmall.reception.pojo.UserAddress;
import shoppingmall.reception.service.MemberAddressService;

import java.util.List;

/**
 * 平台会员用户地址服务层实现类，提供相关地址服务
 */
@SuppressWarnings({"all"})
@Service
public class MemberAddressServiceImpl implements MemberAddressService {

    //平台会员地址mapper层实例
    @Autowired
    private MemberAddressMapper memberAddressMapper;

    /**
     * 根据用户id查询用户地址列表集合
     */
    @Override
    public List<UserAddress> getMemberAddressList(int userId) {
        //调用mapper层方法
        List<UserAddress> memberAddressList = memberAddressMapper.getMemberAddressList(userId);
        return memberAddressList;
    }

    /**
     * 为某个会员用户添加一个收货地址信息
     */
    @Override
    public boolean saveMemberAddress(UserAddress userAddress) {
        //调用mapper层方法
        //判断要保存的地址是否设置为默认的，默认的 则需从用户地址表中查找当前用户默认的地址信息将其改为非默认
        if(userAddress.getDefaultAddress()){
            List<UserAddress> defaultUserAddressList = memberAddressMapper.getDefaultUserAddressList(userAddress.getUserId());
            for(int i = 0; i < defaultUserAddressList.size(); i++){
                //将默认地址修改为非默认的
                UserAddress changeUserAddress = new UserAddress();
                changeUserAddress.setId(defaultUserAddressList.get(i).getId());
                changeUserAddress.setDefaultAddress(!defaultUserAddressList.get(i).getDefaultAddress());
                memberAddressMapper.updateUserAddress(changeUserAddress);
            }
        }

        //添加收货地址
        try {
            memberAddressMapper.saveUserAddress(userAddress);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 更新某个会员用户的某条收货地址信息
     */
    @Override
    public boolean updateMemberAddress(UserAddress userAddress) {
        //调用mapper层方法
        //判断要更新的地址是否设置为默认的，默认的 则需从用户地址表中查找当前用户默认的地址信息将其改为非默认
        if(userAddress.getDefaultAddress()){
            List<UserAddress> defaultUserAddressList = memberAddressMapper.getDefaultUserAddressList(userAddress.getUserId());
            for(int i = 0; i < defaultUserAddressList.size(); i++){
                //将默认地址修改为非默认的
                UserAddress changeUserAddress = new UserAddress();
                changeUserAddress.setId(defaultUserAddressList.get(i).getId());
                changeUserAddress.setDefaultAddress(!defaultUserAddressList.get(i).getDefaultAddress());
                memberAddressMapper.updateUserAddress(changeUserAddress);
            }
        }

        //添加收货地址
        try {
            memberAddressMapper.updateUserAddress(userAddress);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 删除某个会员用户的某条收货地址信息
     */
    @Override
    public boolean deleteMemberAddress(int addressId) {
        //调用mapper层方法
        try{
            memberAddressMapper.deleteUserAddress(addressId);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 查询一条地址信息，根据地址id
     */
    @Override
    public UserAddress getAddressInfoById(int addressId) {
        //调用mapper层方法
        UserAddress addressInfo = memberAddressMapper.getAddressInfoById(addressId);
        return addressInfo;
    }
}
